package com.zhkjnet.zhyy.controller;

import com.zhkjnet.zhyy.bean.Paging;
import com.zhkjnet.zhyy.bean.RequiredPermission;
import com.zhkjnet.zhyy.bean.Roles;
import com.zhkjnet.zhyy.entity.User;
import com.zhkjnet.zhyy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private HttpSession session;

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private RechargeRecordService rechargeRecordService;

    @Autowired
    private OilRechargeService oilRechargeService;

    @Autowired
    private MoneyRecordService moneyRecordService;

    @RequiredPermission(Roles.Admin)
    @GetMapping("")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("admin/index.html");
        return mv;
    }

    @RequiredPermission(Roles.Admin)
    @GetMapping("/user")
    public ModelAndView user(@RequestParam(required = false) Long page) {
        page = page == null ? 1 : page;
        Integer length = 20;
        Paging paging = new Paging(page, length, userService.count(), 5);
        ModelAndView mv = new ModelAndView("admin/user.html");
        mv.addObject("users", userService.findAll(page, length));
        mv.addObject("paging", paging);
        return mv;
    }

    @RequiredPermission(Roles.Admin)
    @GetMapping("/admin")
    public ModelAndView admin() {
        ModelAndView mv = new ModelAndView("admin/admin.html");
        mv.addObject("admins", adminService.findAll());
        return mv;
    }

    @GetMapping("/login")
    public String login() {
        return "admin/login.html";
    }

    /**
     * 全部充值页面
     * @return
     */
    @RequiredPermission(Roles.Admin)
    @GetMapping("/allRecharge")
    public ModelAndView allRecharge(@RequestParam(required = false) Long page) {
        page = page == null || page < 1 ? 1 : page;
        Integer length = 20;
        ModelAndView mv = new ModelAndView("admin/allRecharge.html");
        mv.addObject("recharges", rechargeRecordService.findAll(page, length));
        Paging paging = new Paging(page, length, rechargeRecordService.count(), 5);
        mv.addObject("paging", paging);
        return mv;
    }

    /**
     * 待处理充值页面
     * @return
     */
    @RequiredPermission(Roles.Admin)
    @GetMapping("/unRecharge")
    public ModelAndView unRecharge() {
        ModelAndView mv = new ModelAndView("admin/unRecharge.html");
        mv.addObject("recharges", rechargeRecordService.findAllSubmit());
        return mv;
    }

    /**
     * 退出登陆
     * @return
     */
    @RequiredPermission(Roles.Admin)
    @GetMapping("/logout")
    public String logout() {
        session.removeAttribute("id");
        session.removeAttribute("role");
        return "redirect:login";
    }

    @RequiredPermission(Roles.Admin)
    @GetMapping("/oilRecharges")
    public ModelAndView oilRecharges(@RequestParam(required = false) Long page) {
        page = page == null ? 1 : page;
        Integer length = 20;
        Paging paging = new Paging(page, length, oilRechargeService.count(), 5);
        ModelAndView mv = new ModelAndView("admin/oilRecharges.html");
        mv.addObject("paging", paging);
        mv.addObject("recharges", oilRechargeService.findAll(page, length));
        return mv;
    }

    @RequiredPermission(Roles.Admin)
    @GetMapping("/moneyRecords")
    public ModelAndView moneyRecords(@RequestParam Long userId,@RequestParam(required = false) String message, @RequestParam(required = false) Long page) {
        page = page == null ? 1 : page;
        
        Integer length = 20;
        ModelAndView mv = new ModelAndView("admin/moneyRecords.html");
        Paging paging = new Paging(page, length, moneyRecordService.countByUserId(userId,message), 5);
        mv.addObject("records", moneyRecordService.findAllByUserId(userId, message, page, length));
        mv.addObject("paging", paging);
        mv.addObject("userId", userId);
        mv.addObject("message", message);
        return mv;
    }

}
